package com.jin.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jin.bean.TruckType;
import com.jin.common.ResponseResult;

import com.jin.service.TruckTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created on 2025/5/14.
 * @author Zhongyuan Jin
 */
@RestController
@RequestMapping("/truck-type")
public class TruckTypeController {

    @Autowired
    private TruckTypeService truckTypeService;

    //  获取全部车型
    @GetMapping("/all")
    public ResponseResult<List<TruckType>> getAllTruckTypes() {
        List<TruckType> truckTypes = truckTypeService.list();
        return ResponseResult.success("获取全部车型", truckTypes);
    }
    //查询所有车型（支持分页）
    @GetMapping("/list")
    public ResponseResult<List<TruckType>> list(@RequestParam(defaultValue = "1") int pageNum,
                                           @RequestParam(defaultValue = "10") int pageSize) {
        Page<TruckType> page = new Page<>(pageNum, pageSize);
        Page<TruckType> truckTypePage = truckTypeService.page(page);

        List<TruckType> records = truckTypePage.getRecords();
        long total = truckTypePage.getTotal();
        return ResponseResult.success("查询所有车型（支持分页）", records, total);
    }

    //根据ID获取车型
    @GetMapping("/{id}")
    public ResponseResult<TruckType> getTruckSizeById(@PathVariable Integer id) {
        TruckType truckType = truckTypeService.getById(id);
        if (truckType == null) {
            return ResponseResult.fail("未找到该车型", null);
        }
        return ResponseResult.success("找到该车型", truckType);
    }

    //新增车型
    @PostMapping
    public ResponseResult<Boolean> add(@RequestBody TruckType truckType) {
        boolean result = truckTypeService.save(truckType);
        return new ResponseResult<>(result ? 0 : 1, result ? "添加成功" : "添加失败", result, null);
    }
    //更新车型
    @PutMapping
    public ResponseResult<Boolean> update(@RequestBody TruckType truckType) {

        boolean result = truckTypeService.updateById(truckType);
        return new ResponseResult<>(result ? 0 : 1, result ? "更新成功" : "更新失败", result,  null);
    }

    //删除车型
    @DeleteMapping("/{id}")
    public ResponseResult<Boolean> delete(@PathVariable Integer id) {
        boolean result = truckTypeService.removeById(id);
        return new ResponseResult<>(result ? 0 : 1, result ? "删除成功" : "删除失败", result, null);
    }

    //启用车型
    @PutMapping("/enable/{id}")
    public ResponseResult<Boolean> enable(@PathVariable Integer id) {
        TruckType truckType = truckTypeService.getById(id);
        if (truckType == null) {
            return ResponseResult.fail( "未找到该车型", false);
        }
        truckType.setIsEnable(1); // 启用
        boolean result = truckTypeService.updateById(truckType);
        return new ResponseResult<>(result ? 0 : 1, result ? "启用成功" : "启用失败", result,  null);
    }

    //禁用车型
    @PutMapping("/disable/{id}")
    public ResponseResult<Boolean> disable(@PathVariable Integer id) {
        TruckType truckType = truckTypeService.getById(id);
        if (truckType == null) {
            return ResponseResult.fail("未找到该车型", false);
        }
        truckType.setIsEnable(0); // 禁用
        boolean result = truckTypeService.updateById(truckType);
        return new ResponseResult<>(result ? 0 : 1, result ? "禁用成功" : "禁用失败", result,  null);
    }

    //按名称模糊查询
    @GetMapping("/search")
    public ResponseResult<List<TruckType>> searchByName(@RequestParam String truckTypeName) {
        List<TruckType> truckTypes = truckTypeService.list(new QueryWrapper<TruckType>().like("truck_type_name", truckTypeName));
        return ResponseResult.success( "按名称模糊查询成功", truckTypes);
    }

}